package com.bxs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bxs.global.JsonResult;
import com.bxs.mapper.FilterRuleMapper;
import com.bxs.pojo.FilterRule;
import com.bxs.pojo.MailReceiver;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 抓包过滤规则管理
 */
@RestController
public class FilterRuleController {
    @Autowired
    FilterRuleMapper filterRuleMapper;

    @GetMapping("fileRuleList")
    public JsonResult filterRuleList(@RequestParam Map<String, Object> param){
        QueryWrapper<FilterRule> queryWrapper = new QueryWrapper<>();
        List<FilterRule> filterRules = filterRuleMapper.selectList(queryWrapper);
        return JsonResult.success(filterRules);
    }

    @PostMapping("filterRuleAdd")
    public JsonResult filterRuleAdd(@RequestBody Map<String, Object> param) throws JsonProcessingException {
        System.out.println("filterRuleAdd:" + param);
        param = (Map)param.get("data");
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(param);
        FilterRule data = objectMapper.readValue(s, FilterRule.class);
        filterRuleMapper.insert(data);
        return JsonResult.success(data);
    }

    @GetMapping("filterRuleDelete")
    public JsonResult filterRuleDelete(@RequestParam Map<String, Object> param){
        Integer id = Integer.valueOf((String)param.get("id"));
        int i = filterRuleMapper.deleteById(id);
        return JsonResult.success(i);
    }

    @PostMapping("filterRuleUpdate")
    public JsonResult filterRuleUpdate(@RequestBody Map<String, Object> param) throws JsonProcessingException {
        param = (Map)param.get("data");
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(param);
        FilterRule data = objectMapper.readValue(s, FilterRule.class);
        int i = filterRuleMapper.updateById(data);
        return JsonResult.success(i);
    }
}
